package com.wzt;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

//管道类
public class Column {
    public BufferedImage column;
    public int x;
    public int y;
    public int width;
    public int height;
    public static int count=0;//管道数量(静态属性，多个对象共享，唯一)
    public int distance=270;//每根管道之间的数量
    public Random random = new Random();
    public int gap=144;//管道间隙

    public Column(){
        try {
            column= ImageIO.read(getClass().getResource("/img/column.png"));
            width=column.getWidth();//通过缓存图片获取宽度
            height=column.getHeight();
            x=410+distance*count;//定义每一根管道的x轴
            //y=-200;
            //管道间隙值的范围在100~400之间
            y=random.nextInt(300)+100-height/2;//让y轴取随机值
        } catch (IOException e) {
            e.printStackTrace();
        }
        count++;//让管道每初始化一次就增加一个
    }

    //管道移动
    public void step(){
        x--;
        if(x==-width){
            x=410;
            y=random.nextInt(300)+100-height/2;//重画y，让y多样性
        }
    }
}
